#include "loginrequest.h"
#include "clientnetwork.h"
#include "datacenter.h"
#include "clientcenter.h"
#include "common.h"
#include "singleclient.h"
#include "clientnetwork.h"

LoginRequest::LoginRequest(int clientId, SOCKET socket, const QString& realIp, unsigned short groupFileServerPort, unsigned short videoServerPort, unsigned short audioServerPort):
		ClientRequest(socket, "Login"),
		m_ClientId(clientId),
		m_RealIp(realIp),
		m_GroupFileServerPort(groupFileServerPort),
		m_VideoServerPort(videoServerPort),
		m_AudioServerPort(audioServerPort)
{
}

void LoginRequest::doRequest()
{
	boost::shared_ptr<ClientCenter> clientCenter =  g_DataCenter->createClientCenter();
	ClientCenter::Iterator it = clientCenter->findClient(m_ClientId);
	if(it != clientCenter->end())
	{
		boost::shared_ptr<SingleClient> client = *it;
		client->getNetwork()->setupSocket(m_Socket);
		client->getNetwork()->setRealIpAddress(m_RealIp);
		client->getNetwork()->setGroupFileServerPort(m_GroupFileServerPort);
		client->setVideoServerPort(m_VideoServerPort);
		client->setAudioServerPort(m_AudioServerPort);
		setClient(client);
		doneRequest(Successful);
	}
	else
	{
		doneRequest(Refused);
	}
}
